<?
 if(ereg("Page.class",$_SERVER['PHP_SELF'])) exit();
include_once("../include/dbConnect.php");

class Paging {
	var $offset;             //jumlah rekord yang diambil
	var $total;              //total rekord
	var $total_page;		 //total halaman
	var $firt_page;			 //halaman pertama
	var $last_page;          //halaman terakhir
	var $previous;           //halaman sebelumnya
	var $next_page;          //halaman setelahnya
	var $max_paging;         //maksimal jumlah halaman yang ditampilkan selainnya akan disembunyikan
	var $url;                //url paging
	var $query;				 //query akses datatabase tanpa diserta mysql_query();
	var $var_page;			 //variabel halaman untuk mengakses halaman, misal hal,halaman pada url test.php?hal=6
	var $count_page;		 //total halaman
	var $current_page;       //halaman yang sedang dipilih
	var $img_first="../images/@first.gif";
	var $img_prev="../images/@prev.gif";
	var $img_next="../images/@next.gif";
	var $img_last="../images/@last.gif";
	
	/* This function used to set an images thoose representative of paging buttons/links
	*/
	function setImage($first,$prev,$next,$last)
	{
		$this->first=$first;
		$this->prev=$prev;
		$this->next=$next;
		$this->last=$last;
	}
	
	/* This function used to initialize paging variable
		- $init_query set query will be processed
		- $init_offset set the number offset to show, it's mean the first record to show at a page
		- $max_paging set the number maximum of page shown at button links, like : <first><prev>1 2 3 4 5<next><last>
		  the number 1 until 5 are range created used to $max_paging
		- $page_variable is query string variable that catch to initialize page shown
	*/
	function initPaging($init_query,$init_offset,$max_paging,$page_variable){
		$this->offset=$init_offset;
		$this->max_paging=$max_paging;
		$this->query=$init_query;
		$this->var_page=$page_variable;
		$sql_query=mysql_query($this->query);
		$this->total=mysql_num_rows($sql_query);
		//mysql_free_result($sql_query);
		//echo mysql_error();
		$this->total_page=ceil($this->total/$this->offset);
	}
	
	/*
		This function used to get query after processed, it's not a result query, so you must use mysql_query at your page
		to get a result
	*/
	function getQuery(){
		$curr_page=$_GET[$this->var_page];
		if($curr_page<=0 | empty($curr_page))
			$this->current_page=1;
		else
			$this->current_page=$curr_page;
		
		$this->first_offset=($this->current_page-1)*$this->offset;
		$this->first_offset=($this->first_offset<=0?0:$this->first_offset);
				
		$querypaging=$this->query." limit $this->first_offset, $this->offset";
		return $querypaging;
	}
	
	/*
		this function to get the first offset or number series of record to show at your page
	*/
	function getFirstOffset(){
		return $this->first_offset;
	}
	
	/*
		this function to get the button/links of paging
	*/
	
	function getProcess(){
		
		if($this->current_page<=0 | empty($this->current_page))
			$this->current_page=1;
		
		$this->url=$_SERVER['REQUEST_URI'];
		$this->first_page=1;
		$this->last_page=$this->total_page;

		$this->previous=($this->current_page<=1?1:$this->current_page-1);
		$this->next_page=($this->current_apge>=$this->total_page?$this->current_page:$this->current_page+1);
		$this->first_list=ceil($this->current_page/$this->max_paging)*$this->max_paging-$this->max_paging+1;
		$this->first_list=($this->first_list<$this->fisrt_page?$this->first_page:$this->first_list);
		$this->last_list=$this->first_list+$this->max_paging-1;
		$this->total_page=($this->total_page==0?1:$this->total_page);
		$this->last_list=($this->last_list>=$this->total_page?$this->total_page:$this->last_list);
		
			
		if(ereg("\?",$this->url)){
			$split_url=explode("?",$this->url);
		
			if(ereg("\&",$split_url[1])){
				$split_query_string=explode("&",$split_url[1]);
				foreach($split_query_string as $var){
					if(!ereg($this->var_page,$var) && !ereg("result",$var))
						$sync_query.=$var."&";
				}
				$sync_query_sip=substr($sync_query,0,strlen($sync_query)-1);
				$this->urlcomplete=$split_url[0]."?".$sync_query_sip;
				$this->adj="&";
			}else{
				if(!ereg($this->var_page,$split_url[1]) && !ereg("result",$split_url[1])){
					$this->urlcomplete=$split_url[0]."?".$split_url[1];
					$this->adj="&";
				}else
					$this->adj="?";				
			}
		
		}else{
			$this->adj="?";
			$this->urlcomplete=$this->url;
		}

	}
	
	function getPaging(){
	
	$this->getProcess();
	
	$url_list="<a name=$this->var_page></a><a href=$this->urlcomplete$this->adj$this->var_page=$this->first_page#$this->var_page title='first page'><img src=\"$this->img_first\" border=\"0\" ></a>";
	$url_list.="<a href=$this->urlcomplete$this->adj$this->var_page=$this->previous#$this->var_page title='previous page'><img src=\"$this->img_prev\" border=\"0\" ></a> ";
	$url_list.="<a href=$this->urlcomplete$this->adj$this->var_page=$this->next_page#$this->var_page title='next page' ><img src=\"$this->img_next\" border=\"0\" ></a>";
	$url_list.="<a href=$this->urlcomplete$this->adj$this->var_page=$this->last_page#$this->var_page title='last page'><img src=\"$this->img_last\" border=\"0\" ></a>";	
	
	return $url_list;
	}
	
	function getPagingText(){
		
		$this->getProcess();
		
		$url_list="<a href=$this->urlcomplete$this->adj$this->var_page=$this->first_page title='first page'>First</a>";
		$url_list.=" <a href=$this->urlcomplete$this->adj$this->var_page=$this->previous title='previous page'>Prev</a> ";
		for($m=$this->first_list;$m<=$this->last_list;$m++)
		{
			$url_list.="<a href=$this->urlcomplete$this->adj$this->var_page=$m title='page $m' >$m</a> ";
		}
		$url_list.="<a href=$this->urlcomplete$this->adj$this->var_page=$this->next_page title='next page' >Next</a>";
		$url_list.=" <a href=$this->urlcomplete$this->adj$this->var_page=$this->last_page title='last page'>Last</a>";	
	
		return $url_list;
	}
	
	//get info page
	function getInfoPage(){
		if($this->current_page==1)
			$page="halaman pertama";
		else if($this->current_page==$this->total_page)
			$page="halaman terakhir";
		else
			$page="halaman ke-".$this->current_page;
		
		return ($page." dari  ".$this->total_page." halaman");
			
	}
	
	//get total record
	function getTotal(){
		return $this->total;
	}
}
?>